package com.shuoyue.ycgk.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeFomatUtil {

    /**
     * @param timestamp
     * @param pattern
     * @return
     * @Title: format
     * @Description: 格式化时间
     */
    public static String format(Long timestamp, String pattern) {
        if (timestamp == null || timestamp == 0) {
            return "";
        }
        String result = "";
        try {
            Date date = new Date();
            date.setTime(timestamp);
            SimpleDateFormat format = new SimpleDateFormat(pattern,
                    Locale.getDefault());
            result = format.format(date);
        } catch (Exception e) {
            result = "";
        }
        return result;
    }

    /**
     * @param timeSecond
     * @return
     * @Title: format
     * @Description: 格式化成倒计时格式
     */
    public static String formatHHmmss(long timeSecond) {
        if (timeSecond < 0) {
            return "";
        }
        // 时
        int house = (int) (timeSecond / 3600);
        timeSecond = timeSecond % 3600;
        // 分
        long minute = timeSecond / 60;
        // 秒
        long sec = timeSecond % 60;

        String h = house < 10 ? "0" + house : "" + house;
        String m = minute < 10 ? "0" + minute : "" + minute;
        String s = sec < 10 ? "0" + sec : "" + sec;
        return h + ":" + m + ":" + s;
    }

    /**
     * @param timeSecond
     * @return
     * @Title: format
     * @Description: 格式化成倒计时格式
     */
    public static String formatmmss(long timeSecond) {
        if (timeSecond < 0) {
            return "";
        }
        // 分
        long minute = timeSecond / 60;
        // 秒
        long sec = timeSecond % 60;

        String m = minute < 10 ? "0" + minute : "" + minute;
        String s = sec < 10 ? "0" + sec : "" + sec;
        return m + ":" + s;
    }
}
